<?php

/**
* alltosun.com  service_helper.php for newbd
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* Author: 吴松键 (wusj@alltosun.com)
* Date: 15/2/2 下午5:36
*/

class service_helper
{
    /**
     * 重定义个数
     * @param $order_id
     * @return array
     */
    public static function get_relation_info($order_id)
    {
        $order_relation_list = order_helper::get_order_relation_by_order_id($order_id);
        if (!$order_relation_list) {
            return array();
        }

        $refund_ids     = _model('refund')->getFields('id', array('order_id'=>$order_id, 'refund_status <'=>20,'status'=>1));
        $refund_ids_two = _model('refund')->getFields('id', array('refund_status'=>20, 'type'=>1));
        $refund_ids = array_merge($refund_ids, $refund_ids_two);
        if ($refund_ids) {
            $filter = array(
                'refund_id' => $refund_ids,
                'status'    => 1
            );

            $refund_relation_list = _model('refund_relation')->getList($filter);

            // 重建order_relation_list
            $new_list = array();
            $order_relation_ids = array();
            foreach ($order_relation_list as $k=>$v) {
                $new_list[$v['id']] = $v;
                $order_relation_ids[] = $v['id'];
            }

            $order_ids = array();
            foreach ($refund_relation_list as $k=>$v) {
                if (in_array($v['order_relation_id'], $order_relation_ids)) {
                    $new_list[$v['order_relation_id']]['goods_num'] -= $v['refund_num'];
                    $order_ids[] = $v['order_relation_id'];
                }
            }

            $list = array();
            foreach ($new_list as $k=>$v) {
                if (in_array($k, $order_ids)) {
                    array_push($list, $v);
                } else {
                    array_unshift($list, $v);
                }
            }
            // 重新排序

            return $list;
        }

        return $order_relation_list;
    }

    /**
     *  根据refund_id获取退款退款信息
     */
    public static function get_refund_info($refund_id, $status=1)
    {
        if (!$refund_id) {
            return array();
        }
        $filter['refund_id'] = $refund_id;
        if ($status) {
            $filter['status'] = $status;
        }

        $refund_info = _uri('refund', $refund_id);
        $refund_relation_list = _model('refund_relation')->getList($filter);
        if (!$refund_relation_list) {
            return $refund_info;
        }

        foreach ($refund_relation_list as $k => $v) {
            $snapshot_info = goods_helper::get_snapshot_info($v['order_relation_id']);
            $refund_relation_list[$k]['goods_title'] = $snapshot_info['goods_title'];
            $refund_relation_list[$k]['goods_cover'] = $snapshot_info['sku_cover'];
            $refund_relation_list[$k]['max_num'] = _uri('order_relation', $v['order_relation_id'], 'goods_num');
        }

        $refund_info['detail'] = $refund_relation_list;
        $refund_info['images'] = self::get_refund_image($refund_id);

        return $refund_info;
    }

    /**
     * 获取退货描述图片
     */
    public static function get_refund_image($refund_id)
    {
        if (!$refund_id) {
            return array();
        }

        $filter = array(
            'res_name' => 'service',
            'res_id'   => $refund_id
        );

        $file_ids = _model('attachment_relation')->getFields('attachment_id', $filter);
        if (!$file_ids) {
            return array();
        }
        $file_list = _model('attachment')->getList(array('id'=>$file_ids));

        return $file_list;
    }

    /**
     * 预算退款总金额
     */
    public static function update_refund_total($refund_id)
    {
        if (!$refund_id) {
            return 0;
        }

        $refund_info   = _uri('refund', $refund_id);
        $order_info    = _uri('order', $refund_info['order_id']);
        $relation_list = _model('refund_relation')->getList(array('refund_id'=>$refund_id, 'status'=>1));
        $refund_price = 0;
        foreach ($relation_list as $k=>$v) {
            $refund_price += $v['refund_price'] * $v['refund_num'];
        }

        // 获取是否使用优惠券
        $has_coupon    = _uri('refund', array('order_id'=>$order_info['id'], '`id` <>'=>$refund_id,'type'=>1, 'refund_status'=>25, 'is_coupon'=>1), 'id');
        $has_promotion = _uri('refund', array('order_id'=>$order_info['id'], '`id` <>'=>$refund_id,'type'=>1, 'refund_status'=>25, 'is_promotion'=>1), 'id');

        if (!$has_promotion) {
            $refund_price -= $order_info['promotion_offer_price'];
        }
        if (!$has_coupon) {
            $refund_price -= $order_info['coupon_offer_price'];
        }

        return $refund_price;
    }

    /**
     * 根据订单id 获取退款状态
     */
    public static function get_refund_status($order_id)
    {
        if (!$order_id) {
            return array();
        }

        $refund_info = _model('refund')->read(array('order_id'=>$order_id, 'type'=>1));

        if ($refund_info) {
            return array(
                'status' => $refund_info['refund_pay_status'],
                'refund_time' => $refund_info['refund_time'],
                'end_time' => $refund_info['update_time']
            );
        }

        return array();
    }

    /**
     * 获取退款状态
     */
    public static function auto_refund_status($refund_id)
    {
        if (!$refund_id) {
            return;
        }

        $refund_info = _uri('refund', $refund_id);
        if (!$refund_info) {
            return;
        }

        if ($refund_info['type'] == 1 && $refund_info['refund_pay_status'] == 1) {
            $order_info = _uri('order', $refund_info['order_info']);
            $pay_type   = $order_info['type'];
            if ($url = openapi_config::$pay_type[$pay_type]['query']) {
                an_curl(AnUrl($url));
            }
        }
    }



    /**
     * 根据订单号获取最新状态
     */
    public static function get_service_info($order_id)
    {
        if (!$order_id) {
            return '';
        }

        $refund_info = _model('refund')->getList(array('order_id'=>$order_id), ' ORDER BY `add_time` DESC LIMIT 1 ');
        if (!$refund_info) {
            return '';
        }
        if ($refund_info['status'] == 0) {
            return '';
        }
        $refund_info = $refund_info[0];


        $status = '';
        if ($refund_info['type'] == 1){
            $status = refund_config::$refund_status_list[$refund_info['refund_status']];
        } elseif ($refund_info['type'] == 2) {
            $status = barter_config::$barter_status_list[$refund_info['refund_status']];
        } else {
            $status = '维修申请成功';
        }

        return array('id'=>$refund_info['id'], 'status'=>$status);
    }

    /*
     * 获取更新
     */
    public static function get_view_type()
    {
        $filter = array(
            'is_view'=>0, 'user_id'=>user_helper::get_user_id()
            );
        $list = _model('refund')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 1 ');

        if ($list) {
            return $list[0]['type'];
        }

        return 0;
    }
    /**
     * 更新状态
     */

    public static function update_view_status($type)
    {
        if (!$type) {
            return;
        }

        _model('refund')->update(array('is_view'=>0, 'user_id'=>user_helper::get_user_id() ,'type'=>$type), array('is_view'=>1));
    }

    /**
     * 获取商品名称
     */
    public static function get_goods_title($refund_id)
    {
        if (!$refund_id) {
            return '';
        }

        $order_id = _uri('refund', $refund_id, 'order_id');
        if (!$order_id) {
            return '';
        }

        $sku_snapshot_ids = _model('order_relation')->getFields('sku_snapshot_id', array('order_id'=>$order_id));
        if (!$sku_snapshot_ids) {
            return '';
        }

        $goods_title_arr = _model('sku_snapshot')->getFields('goods_title', array('id'=>$sku_snapshot_id));
        if (!$goods_title_arr) {
            return '';
        }

        return join('、', $goods_title_arr);
    }
}